﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;

using VirtualDigibox.Common;
using VirtualDigibox.BusinessService;

namespace VirtualDigibox.Controls.Settings
{
    public partial class SettingsVLCControl : UserControl, ISettingsControl
    {
        [DllImport("shell32.dll")]
        static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken,
           uint dwFlags, [Out] StringBuilder pszPath);


        #region Construcion
        public SettingsVLCControl()
        {
            InitializeComponent();

        } 
        #endregion Construcion

        #region Events
        private void btnVLCDialog_Click(object sender, EventArgs e)
        {

            openFileDialogVLC.FileName = tbVLCPath.Text;
            openFileDialogVLC.InitialDirectory = System.IO.Path.GetDirectoryName(openFileDialogVLC.FileName);
            DialogResult result = openFileDialogVLC.ShowDialog();
            if (result == DialogResult.OK)
            {
                tbVLCPath.Text = openFileDialogVLC.FileName;
            }
        }

        private void btnDefaultSavePath_Click(object sender, EventArgs e)
        {
            if (System.IO.Directory.Exists(tbSavePath.Text))
                folderBrowserDialogSave.SelectedPath = tbSavePath.Text;

            DialogResult folderResult = folderBrowserDialogSave.ShowDialog();
            if (folderResult == DialogResult.OK)
            {
                tbSavePath.Text = folderBrowserDialogSave.SelectedPath;
            }
        } 
        #endregion Events

        #region ISettingsControl Members

        public void SaveData()
        {
            SettingsManager.VLCEXE = tbVLCPath.Text;
            SettingsManager.VideoSavePath = tbSavePath.Text;
            SettingsManager.VLCArgs = tbArgs.Text;
            SettingsManager.FileNameTemplate = tbFileNameTemplate.Text;
        }

        public void LoadData()
        {
            tbVLCPath.Text = SettingsManager.VLCEXE;

            tbSavePath.Text = SettingsManager.VideoSavePath;
            tbArgs.Text = !string.IsNullOrEmpty(SettingsManager.VLCArgs) ? SettingsManager.VLCArgs : tbArgs.Text;
            tbFileNameTemplate.Text = !string.IsNullOrEmpty(SettingsManager.FileNameTemplate) 
                ? SettingsManager.FileNameTemplate : tbFileNameTemplate.Text; 

            if (string.IsNullOrEmpty(tbSavePath.Text))
            {
                StringBuilder sb = new StringBuilder();
                SHGetFolderPath(IntPtr.Zero, Common.Constants.SpecialFolders.MY_VIDEOS, IntPtr.Zero, 0x0000, sb);
                if (System.IO.Directory.Exists(sb.ToString()))
                {
                    tbSavePath.Text = sb.ToString();
                }
            }
        }

        #endregion

        
    }
}
